package com.china08.yxyapi.api.manage.edu.platform.statistics.resourcepandect1;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResPhase;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResPhaseRepository;
import com.china08.yxyapi.service.yxyresource.statistics.StatisticsEachSchoolService;

/**
 * 
 * @Description 教育局平台办公 资源统计（饼状图）资源学段分布
 *
 * @Author LiuWeidong
 *
 * @Date 2017年12月8日
 *
 */

@CrossOrigin
@RestController
public class ResourcePhase1CountController extends YxyApiBaseController {

    @Autowired
    private StatisticsEachSchoolService statisticsEachSchoolService;
    @Autowired
    private YxyResPhaseRepository yxyResPhaseRepository;

    @RequestMapping(value = "/api_yw/manage/edu/platform/statistics/resourcepandect1/phasecount/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response) {

        RespModel model = new RespModel();
        Integer schoolBureauLevel = getProfile(request, response).getSchoolBureauLevel();
        String districtCode = null;
        if (schoolBureauLevel == 0) {
            districtCode = getProfile(request, response).getSchoolAreaIdDistrict();
        } else if (schoolBureauLevel == 1) {
            districtCode = getProfile(request, response).getSchoolAreaIdCity();
        } else if (schoolBureauLevel == 2) {
            districtCode = getProfile(request, response).getSchoolAreaIdProvince();
        }
        Map<String, Long> map = statisticsEachSchoolService.resourcephaseCount(districtCode, schoolBureauLevel);
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        TermQueryBuilder builderType2 = null;
        boolQueryBuilder.mustNot(builderType2);

        YxyResPhase yxyResPhase = null;
        Long zong = 0L;
        List<Object[]> objectsLst = new ArrayList<Object[]>();
        DecimalFormat df = new DecimalFormat("######0.00");
        long long1 = 0;
        Object[] obs = null;
        for (Map.Entry<String, Long> entry1 : map.entrySet()) {
            zong += Long.decode(entry1.getValue().toString());
        }
        for (Map.Entry<String, Long> entry : map.entrySet()) {
            obs = new Object[2];
            // 1.将分组后的map集合获取到key值也就是id值，然后根据id获取到字段名称
            yxyResPhase = yxyResPhaseRepository.findById(entry.getKey());
            if (yxyResPhase != null) {
                // nameLst.add(yxyResPhase.getName());
                obs[0] = yxyResPhase.getName();
                long1 = Long.decode(entry.getValue().toString());
                // 将double类型的数据保存到小数点的后两位(计算的是每个学段占总共的百分比)
                obs[1] = Double.parseDouble(df.format((double) long1));
                objectsLst.add(obs);
                System.out.println("每个学段对应的数据" + Integer.parseInt(entry.getValue().toString()) + "学段名称：" + yxyResPhase.getName());
            }

        }

        String[] phaseNameList = new String[] { "幼儿园", "小学", "初中", "高中", "大学" };
        List<Object[]> subList = new ArrayList<Object[]>();
        if (objectsLst.size() > 0) {
            Object[] obj = null;
            for (String subjectName : phaseNameList) {

                for (Object[] objects : objectsLst) {
                    if (subjectName.equals(objects[0])) {
                        obj = new Object[2];
                        obj[0] = objects[0];
                        obj[1] = objects[1];
                        subList.add(obj);
                    }
                }
            }

        }
        // 将新组合的list里的学段名称值连接在一起
        String phaseName = "";
        for (Object[] objects : subList) {
            phaseName += objects[0] + ",";
        }
        if (objectsLst.size() > subList.size()) {
            for (Object[] countLstObj : objectsLst) {

                // 搜索字符串中是否包含数组里的元素
                if (phaseName.indexOf(countLstObj[0].toString()) == -1) {
                    subList.add(countLstObj);
                }
            }
        }
        model.objectsLst = subList;
        return model;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 字段名和每个学段对应的数量
        public List<Object[]> objectsLst;
    }

}
